home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / CModalDialog Class / Sample Code / CTextBoxDialog.cp next >
Encoding:
Text File  |  1995-10-11  |  6.0 KB  |  220 lines  |  [TEXT/CWIE]

  1. // CTextBoxDialog.cp
  2. //     Copyright © 1995 by Michael F. Kamprath, All rights reserved.
  3. //
  4. // Contact information:
  5. //     mailto:kamprat@leonardo.net
  6. //     http://www.leonardo.net/kamprath
  7. //
  8. // License:
  9. //     This code may be freely used in free and shareware products. Commercial product
  10. //     users must supply me with a free, fully licensed copy of the product this code is
  11. //     used in. In either case, users should notify me of their use of this code.
  12. //
  13. //     This code may be freely distributed in it's non-modified form. It's original archive
  14. //     should be kept intact.
  15. //
  16. // Version History:
  17. //     v1.0.0 - Inititial release.
  18. //
  19.  
  20. #include <Dialogs.h>
  21. #include <Controls.h>
  22. #include "CTextBoxDialog.h"
  23.  
  24. pascal void     TextBoxItem( WindowPtr theWindow, short itemNo);
  25. pascal void     TextScrollProc(ControlHandle theControl, short partCode);
  26.  
  27. CTextBoxDialog::CTextBoxDialog( short dlogResID, short textResID )
  28.     :    CModalDialog( dlogResID, CTextBoxDialog::kOK, CTextBoxDialog::kOK )
  29. {
  30. short    itemType, height;
  31. Handle    itemHandle, textH, styleH;
  32. Rect    itemRect, textRect;
  33.  
  34.     this->textBoxUPP = NewUserItemProc( TextBoxItem );
  35.     this->GetItemInfo(kTextBox, &itemType, &itemHandle, &textRect );
  36.     this->SetItemInfo(kTextBox, kUserDialogItem, (Handle)this->textBoxUPP, &textRect );
  37.     this->SetDialogPort();
  38.     this->aboutTEH = ::TEStyleNew(&textRect, &textRect);
  39.     textH = ::Get1Resource('TEXT',textResID);
  40.     styleH = ::Get1Resource('styl',textResID);
  41.     HLock( textH );
  42.     ::TEStyleInsert( *textH, GetHandleSize(textH), (StScrpHandle)styleH, this->aboutTEH);
  43.     HUnlock( textH );
  44.     ReleaseResource( textH );
  45.     ReleaseResource( styleH );
  46.     ::TECalText(this->aboutTEH);
  47.     ::TEAutoView(true, this->aboutTEH);
  48.     ::TEActivate(this->aboutTEH);
  49.         
  50.     height = ::TEGetHeight((*this->aboutTEH)->nLines, 0, this->aboutTEH);
  51.     this->GetItemInfo(kScrollBar, &itemType, &itemHandle, &itemRect );
  52.     ::SetControlMaximum((ControlHandle)itemHandle, height - (textRect.bottom - textRect.top) );
  53.     
  54.     this->scrollProcUPP = NewControlActionProc(TextScrollProc);
  55. }
  56.  
  57. CTextBoxDialog::~CTextBoxDialog()
  58. {    
  59.     DisposeRoutineDescriptor((UniversalProcPtr)this->textBoxUPP);
  60.     DisposeRoutineDescriptor((UniversalProcPtr)this->scrollProcUPP);
  61.     ::TEDispose(this->aboutTEH);
  62. }
  63.  
  64. void
  65. CTextBoxDialog::DisplayTextBoxDialog( void )
  66. {
  67. short            itemHit;
  68. Boolean            done = false;
  69.  
  70.     this->Show();
  71.     
  72.     while (!done)
  73.     {
  74.         itemHit = this->DoOneModalLoop();
  75.         
  76.         switch (itemHit)
  77.         {
  78.             case kOK:
  79.                 done = true;
  80.                 break;
  81.             case kScrollBar:
  82.                 break;
  83.         
  84.         }
  85.     }
  86.     
  87.     this->Hide();
  88. }
  89.  
  90. Boolean        
  91. CTextBoxDialog::KeyDownFilter( DialogPtr theDlg, EventRecord *theEvent, short *itemHit)
  92. {
  93. Boolean        returnCode = false;
  94. char        key = theEvent->message&charCodeMask;
  95.  
  96.     if (theEvent->modifiers&cmdKey)
  97.     {
  98.         if ((key == 'c')||(key == 'C'))
  99.         {
  100.             ::TECopy(this->aboutTEH);
  101.             returnCode = true;
  102.         }
  103.         else if ((key == 'a')||(key == 'A'))
  104.         {
  105.             ::TESetSelect(0, 32767, this->aboutTEH);
  106.             returnCode = true;
  107.         }
  108.     }
  109.     
  110.     if (!returnCode)
  111.         returnCode = this->CModalDialog::KeyDownFilter( theDlg, theEvent, itemHit);
  112.     
  113.     return returnCode;
  114. }
  115.  
  116. Boolean
  117. CTextBoxDialog::MouseDownFilter(    DialogPtr         theDlg, 
  118.                                     EventRecord*     theEvent, 
  119.                                     short*             itemHit )
  120. {
  121. ControlHandle    theControl;
  122. GrafPtr        savePort;
  123. short        foundItem, thePart;
  124. short        itemType, newVal;
  125. Handle        itemHandle;
  126. Rect        itemRect;
  127. Point        thePt;
  128. Boolean        returnCode = false;
  129.  
  130.     ::GetPort( &savePort );
  131.     ::SetPort( theDlg );
  132.     thePt = theEvent->where;
  133.     ::GlobalToLocal( &thePt );
  134.  
  135.     this->GetItemInfo( kTextBox, &itemType, &itemHandle, &itemRect );
  136.     if (::PtInRect(thePt, &itemRect))
  137.     {
  138.         ::TEClick(thePt, theEvent->modifiers&shiftKey ? true : false, this->aboutTEH);
  139.         this->GetItemInfo( kScrollBar, &itemType, &itemHandle, &itemRect );
  140.         newVal = (*this->aboutTEH)->viewRect.top - (*this->aboutTEH)->destRect.top;
  141.         ::SetControlValue( (ControlHandle)itemHandle, newVal );
  142.         *itemHit = kTextBox;
  143.         returnCode = true;
  144.     }
  145.     else
  146.     {
  147.         this->GetItemInfo( kScrollBar, &itemType, &itemHandle, &itemRect );
  148.         foundItem = ::FindControl(thePt, theDlg, &theControl);
  149.         if (theControl == (ControlHandle)itemHandle)
  150.         {
  151.             if (foundItem == kControlIndicatorPart)
  152.             {
  153.                 this->lastScrollValue = ::GetControlValue( theControl );
  154.                 thePart = ::TrackControl(theControl, thePt, nil);
  155.                 newVal = ::GetControlValue( theControl );
  156.                 ::TEPinScroll(0, this->lastScrollValue - newVal, this->aboutTEH);
  157.             }
  158.             else            
  159.                 thePart = ::TrackControl(theControl, thePt, this->scrollProcUPP);
  160.             if (thePart)
  161.                 *itemHit = kScrollBar;
  162.         
  163.             returnCode = true;
  164.         }
  165.     }
  166.     
  167.     ::SetPort( savePort );
  168.     
  169.     if (!returnCode)
  170.         returnCode = this->CModalDialog::MouseDownFilter( theDlg, theEvent, itemHit);
  171.         
  172.     return returnCode;
  173. }
  174.  
  175. pascal void        TextBoxItem( WindowPtr theWindow, short /* itemNo */)
  176. {
  177. short                itemType;
  178. Handle                itemHandle;
  179. Rect                itemRect;
  180. CTextBoxDialog        *theThis = (CTextBoxDialog *)GetWRefCon( theWindow );
  181.  
  182.     theThis->GetItemInfo( CTextBoxDialog::kTextBox, &itemType, &itemHandle, &itemRect );
  183.     
  184.     ::TEUpdate(&itemRect, theThis->aboutTEH);
  185.     
  186.     MoveTo( itemRect.right, itemRect.top - 1);
  187.     LineTo( itemRect.left - 2, itemRect.top - 1); 
  188.     LineTo( itemRect.left - 2, itemRect.bottom + 1); 
  189.     LineTo( itemRect.right, itemRect.bottom + 1); 
  190. }
  191.  
  192. pascal void     TextScrollProc(ControlHandle theControl, short partCode)
  193. {
  194. short                newVal;
  195. CTextBoxDialog        *theThis;
  196.  
  197.     HLock( (Handle)theControl );
  198.     theThis = (CTextBoxDialog *)GetWRefCon( (*theControl)->contrlOwner );
  199.     HUnlock( (Handle)theControl );
  200.     theThis->lastScrollValue = GetControlValue( theControl );
  201.     switch (partCode)
  202.     {
  203.         case kControlUpButtonPart:
  204.             SetControlValue( theControl, GetControlValue( theControl ) - 10 );
  205.             break;
  206.         case kControlDownButtonPart:
  207.             SetControlValue( theControl, GetControlValue( theControl ) + 10 );
  208.             break;
  209.         case kControlPageUpPart:
  210.             SetControlValue( theControl, GetControlValue( theControl ) - 100 );
  211.             break;
  212.         case kControlPageDownPart:
  213.             SetControlValue( theControl, GetControlValue( theControl ) + 100 );
  214.             break;
  215.     }
  216.  
  217.     newVal = GetControlValue( theControl );
  218.     TEPinScroll(0, theThis->lastScrollValue - newVal, theThis->aboutTEH);
  219.     theThis->lastScrollValue = newVal;
  220. }